package com.cqrt.util;

import ma.glasnost.orika.MappingContext;
import ma.glasnost.orika.converter.BidirectionalConverter;
import ma.glasnost.orika.metadata.Type;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

public class DateToLocalDateTimeConverter 
    extends BidirectionalConverter<Date, LocalDateTime> {

    @Override
    public LocalDateTime convertTo(
        Date source, 
        Type<LocalDateTime> destinationType, 
        MappingContext mappingContext
    ) {
        if (source == null) {
            return null;
        }
        return source.toInstant()
            .atZone(ZoneId.systemDefault())
            .toLocalDateTime();
    }

    @Override
    public Date convertFrom(
        LocalDateTime source, 
        Type<Date> destinationType, 
        MappingContext mappingContext
    ) {
        if (source == null) {
            return null;
        }
        return Date.from(
            source.atZone(ZoneId.systemDefault())
                .toInstant()
        );
    }
}